function createAddressContainer()
{
    if(!dijit.byId('addressBorderContainer'))
    {
        var bc = new dijit.layout.BorderContainer({
            id:'addressBorderContainer',
            design:'sidebar',
            gutters:true,
            liveSplitters:true,	
            style:'border: 0px; height: 400px;' 
        });
        
        var center = new dijit.layout.ContentPane({id: "bcAddressForm", region: "center", splitter: true, style:"padding: 0px;"});
        bc.addChild(center);
        
        var top = new dijit.layout.ContentPane({id: "bcAddressGrid", region: "top", splitter: true, style:"padding: 0px; height:200px;"});
        bc.addChild(top);
        
        top.set('content', 'top');
        center.set('content', 'bottom');
        
        bc.startup();
        bc.layout();
        
        var myCenter= dijit.byId("myCenter");
        myCenter.set('content', bc);
    }
}

function getAddressForm()
{
    if(!dijit.byId('addressForm'))
    {   
        dojo.xhrGet({
            url:"index.php/address/address/edit",
            handleAs:"text",
            load: function(data){
    //            console.log(data);
                dijit.byId("bcAddressForm").set('content', data);
            }   
        });

        dojo.parser.parse(dojo.byId('bcAddressForm'));
        
    }
}

function onAddressGridClick(e)
{
    var item = e.grid.getItem(e.rowIndex);
//    console.log(item);
    dijit.byId('addressForm').set('value', item);
}

function saveAddressData()
{
    dojo.xhrPost({
        url: 'index.php/address/address/edit?format=json',
        form: 'addressForm',
        load: function(data){
            console.log(data);
            refreshAddressGrid();
        }
    });
}

function changeCustomer()
{
    var customer_id = dijit.byId('customer_id');
//    console.log(customer_id.value);
     dojo.xhrGet({
        url: 'index.php/address/address/view/address_id/'+customer_id.value+'?format=json',
        load: function(data){
            var obj = JSON.parse(data);
//            console.log(obj.item.name1);
            dijit.byId('address_name1').set('value', obj.item.name1);
            dijit.byId('address_name2').set('value', obj.item.name2);
            dijit.byId('address_street').set('value', obj.item.street);
            dijit.byId('address_zipcode').set('value', obj.item.zipcode);
            dijit.byId('address_city').set('value', obj.item.city);
        }
    });
}

//function saveAddress()
//	    {
//		var addressForm = dijit.byId('addressForm');
//		console.log(addressForm.attr('value'));
//		dojo.xhrPost({
//		    url: 'index.php/address/address/list?json',
//		    form: 'addressForm',
//		    load: function(data){
//			console.log(data);
//		    }
//		});
//	    }
//var cellValue;
//function onSet(item, attribute, oldValue, newValue)
//{
//    console.log(item.id[0]);
//    console.log(attribute);
//    console.log(oldValue);
//    console.log(newValue);
//}
//function dblclick(e)
//{
//    var content = "";
//    var item = e.grid.getItem(e.rowIndex);
//    var myDialog = dijit.byId('addressDialog');
//    if(!myDialog){
//
//        dojo.xhrGet({
//            url:"ajax.php?action=getForm&data=address",
//            handleAs:"text",
//            load: function(data){
//                myDialog = new dijit.Dialog({
//                    title: "Programmatic Dialog Creation",
//                    style: "width: 300px",
//                    id: "addressDialog",
//                    content: ""
//                });
//                myDialog.set("content", data);
//
//
//                //myDialog.set("content", url: 'ajax.php?action=getForm&data=address');
//            }
//        });
//
//    }
//
//
//    dijit.byId('addressForm').set('value', item);
//    myDialog.show();
//}
//	    
//function onStartEdit(inCell, inRowIndex)
//{
////		console.log(inCell);
////		console.log(inRowIndex);
//}
//function onApplyCellEdit(inValue, inRowIndex, inFieldIndex)
//{
//
////		console.log(inFieldIndex);
//}

function refreshAddressGrid()
{
    var store = new dojo.data.ItemFileWriteStore({url: 'index.php/address/address/list?format=json'});
    var grid = dijit.byId("gridAddress");
    grid.setStore(store);
    
}

function openAddress()
{
    createAddressContainer();
    getAddressForm();

    var store = new dojo.data.ItemFileWriteStore({url: 'index.php/address/address/list?format=json'});

    /*set up layout*/
    var layout = [[
        {'name': '#', 'field': 'address_id', 'width': '20px'},
        {'name': 'Name1', 'field': 'name1', 'width': '100px',  editable: true},//
        {'name': 'Name2', 'field': 'name2', 'width': '100px'},
        {'name': 'Straße', 'field': 'street', 'width': '180px'},
        {'name': 'PLZ', 'field': 'zipcode', 'width': '50px'},
        {'name': 'Ort', 'field': 'city', 'width': '100px'},
    ]];

    var grid = dijit.byId("gridAddress");
//		    console.log(grid);
    if(!grid)
    {
        /*create a new grid:*/
        grid = new dojox.grid.DataGrid({
            id: 'gridAddress',
            store: store,
            structure: layout,
            rowSelector: '20px'},
            document.createElement('div')
        );
//		    dojo.connect(grid, 'onStartEdit', 'onStartEdit');
//		    dojo.connect(grid, 'onApplyCellEdit', 'onApplyCellEdit');
        dojo.connect(grid, 'onRowClick', 'onAddressGridClick');
        /*append the new grid to the div*/
        dijit.byId("bcAddressGrid").set( 'content', grid.domNode );
        /*Call startup() to render the grid*/
        grid.startup();
    } else{
        grid.setStore(store);
    }


}